<?php

/**
 * IndexController.class.php home模块前台显示控制器
 * Created Time 2016年3月11日 下午5:37:23
 *
 * @author     late.xiao@qq.com
 */
namespace Home\Controller;

use Home\Common\FrontController;
use Base\Lib\ApiRpc;

class IndexController extends FrontController {
    public $limit = 10;

    public function index()
    {

        $request = I('get.');
        try
        {
            $seo = $this->__getSeo();
            $this->assign('seo', $seo);
            $params['page_no'] = isset($request['p']) ? $request['p'] : 1;
            $params['page_size'] = $this->limit;
            $params['order'] = 'aid desc';
            
            if ($request['k'])
            {
                $params['keyword'] = $request['k'];
            }
            if ($request['cid'])
            {
                $params['node_id'] = $request['cid'];
            }
            $params['fields'] = 'title,atid,aid,content,created';
            $params['ifpub'] = 1;
            $data = $this->__getList($params);
            if (! $data)
            {throw new \Exception('好抱歉，还没有文章哦');}
            
            $this->__formatList($data['list']);
            $this->assign('list', $data['list']);
            $this->__pager($params['page_no'], $data['total'], $request);
            $this->assign('search_url', U('Index/index'));
        } catch ( \Exception $e )
        {
            $this->assign('notice', $e->getMessage());
        }
        
        // 获取文章分类
        $this->__getCatList();
        $this->loadView();
    }

    public function article()
    {

        try
        {
            $aid = I('get.id');
            if (! $aid)
            {return $this->_redirectError();}
            
            $params['fields'] = 'title,atid,aid,content,created,node_id';
            $params['aid'] = intval($aid);
            $data = ApiRpc::get('Home')->rpcCall('article.get', $params);
            if (! $data)
            {return $this->_redirectError();}
            $seo['title'] = $data['title'];
            $seo['descr'] = strip_tags(htmlspecialchars_decode($data['content']));
            $seo['descr'] = rsubstr($seo['descr'], 200);
            $this->assign('seo', $seo);
            $this->assign('data', $data);
        } catch ( \Exception $e )
        {
            return $this->_redirectError();
        }
        // 获取文章分类
        $this->__getCatList();
        $this->loadView('show');
    }

    public function _empty($param)
    {

        return $this->_redirectError();
    }

    private function __getList($params)
    {

        $data = ApiRpc::get('Home')->rpcCall('article.get.list', $params);
        
        return $data;
    }

    private function __formatList(&$data)
    {

        if (! $data) return false;
        $tmp = array();
        foreach ($data as $val)
        {
            if ($val['atid'])
            {
                $val['at_url'] = set_attr_url($val['atid']);
            }
            
            $val['remark'] = strip_tags(htmlspecialchars_decode($val['content']));
            $val['remark'] = rsubstr($val['remark'], 200);
            $val['view_url'] = U('Index/article', array(
                                                        'id' => $val['aid'] 
            ));
            $tmp[] = $val;
        }
        
        $data = $tmp;
        
        return true;
    }

    private function __getCatList()
    {

        $params['fields'] = 'node_id,node_name';
        $data = ApiRpc::get('Home')->rpcCall('article.get.node.list', $params);
        $this->assign('cat_list', $data);
        return true;
    }
    
    // 分页
    private function __pager($page, $total, $filter = array())
    {

        $pre_page = '<a href="%s" class="btn btn-warning"><i class="glyphicon glyphicon-chevron-left"></i>&nbsp;</a>';
        $next_page = '<a href="%s" class="btn btn-warning"><i class="glyphicon glyphicon-chevron-right"></i>&nbsp;</a>';
        $c_page = '<button type="button" class="btn btn-warning"><span>第%d页/共%d页</span></button>';
        $page = intval($page);
        $page = $page > 0 ? $page : 1;
        $output = '';
        
        if ($page == $total && $total == 1)
        {
            $output = sprintf($c_page, $page, $total);
        }
        elseif ($page >= $total)
        {
            $filter['p'] = $total - 1;
            $url = $this->create_url($filter);
            $output .= sprintf($pre_page, $url);
            $output .= sprintf($c_page, $total, $total);
        }
        elseif ($page < $total && $page == 1)
        {
            $filter['p'] = $page + 1;
            $url = $this->create_url($filter);
            $output .= sprintf($c_page, $page, $total);
            $output .= sprintf($next_page, $url);
        }
        else
        {
            
            $filter['p'] = $page - 1;
            $url = $this->create_url($filter);
            $output .= sprintf($pre_page, $url);
            $output .= sprintf($c_page, $page, $total);
            $filter['p'] = $page + 1;
            $url = $this->create_url($filter);
            $output .= sprintf($next_page, $url);
        }
        
        $this->assign('pager', $output);
    }

    private function create_url($filter)
    {

        $url = U('Home/Index/index', $filter);
        
        return $url;
    }

    private function __getSeo()
    {

        $params['key'] = 'seo';
        $data = ApiRpc::get('Home')->rpcCall('setting.get', $params);
        
        return $data;
    }

}
